package statistics

import (
	"data/config"
	"data/view/common"
	"data/view/paper"
	"data/view/question"
	"data/view/user"
	"public/message"
	"public/mysqldb"
	"public/tools"
	"strings"

	"github.com/ant0ine/go-json-rest/rest"
)

/*
	成绩分析
*/
func ScoreAnalysis(w rest.ResponseWriter, r *rest.Request) {
	var req paper.GetPaperDetailParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Paper_no) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	_, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())
	var exam paper.Examination_tbl
	orm.Where("pid = ? and is_vaild = 0", req.Paper_no).Find(&exam)

	answers, score := paper.GetPaperAnswerer(req.Paper_no)
	var basic Paper_basic_analysis
	basic.Paper_no = req.Paper_no
	basic.Name = exam.Name
	basic.Topic = exam.Topic
	basic.Bundle = exam.Bundle
	basic.Pass_score = exam.Pass_score
	_, paper_sum_score := paper.GetPaperQuestions(exam.Questions) //试卷满分
	basic.Sum_score = paper_sum_score
	basic.Limit_time = exam.Limit_time
	basic.Answerer_count = len(answers) //应考人数
	basic.Min_score = score.Min_score   //最低分
	basic.Max_score = score.Max_score   //最高分

	var all_sum_score, all_sum_time, pass_count float64 = 0, 0, 0
	if len(answers) > 0 {
		for _, v := range answers {
			all_sum_score += v.Answerer_score
			all_sum_time += v.Answer_time
			if v.Answerer_score >= exam.Pass_score {
				pass_count++
			}
		}
	}
	basic.Average_score = all_sum_score / float64(len(answers))
	basic.Average_time = all_sum_time / float64(len(answers))
	basic.Pass_rate = pass_count / float64(len(answers))

	mp := make(map[string]interface{})
	mp["basic"] = basic
	msg := message.GetSuccessMsg(message.NormalMessageId)
	msg.Data = mp
	w.WriteJson(msg)
}

/*
	单选试题分析
*/
func QuestionAnalysis(w rest.ResponseWriter, r *rest.Request) {
	var req QuestionAnalysisParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Paper_no) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	_, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var paper paper.Examination_tbl
	orm.Where("pid = ? ", req.Paper_no).Find(&paper)
	if paper.Id == 0 {
		w.WriteJson(message.GetErrorMsg(message.NotFindError))
		return
	}
	quests := strings.Split(paper.Questions, common.SPLIT)
	var tmp []question.Question_tbl
	var result []Quest_analysis
	sql := orm.Where("quest_no in (?)", quests).Where("bundle = ?", common.SCHOICE_TYPE_C)

	if req.Quest_no != "" {
		sql = sql.Where("quest_no = ?", req.Quest_no)
	}
	if req.Title != "" {
		sql = sql.Where("title like ?", "%"+req.Title+"%")
	}
	if req.Content != "" {
		sql = sql.Where("content like ?", "%"+req.Content+"%")
	}
	if req.Difficulty != "" {
		sql = sql.Where("difficulty = ?", req.Difficulty)
	}
	sql.Find(&tmp)
	if len(tmp) > 0 {
		for _, v := range tmp {
			var t Quest_analysis
			t.Quest_no = v.Quest_no
			t.Title = v.Title
			t.Content = v.Content
			t.Answer = v.Answer
			t.Analysis = v.Analysis
			t.Right_count, t.Answer_count, t.Right_rate = GetQuestAnswerData(v.Quest_no, v.Answer)
			t.Answer_detail = GetOneOptionData(t.Quest_no, t.Answer_count, strings.Split(v.Option, common.SPLIT))
			result = append(result, t)
		}
	}
	msg := message.GetSuccessMsg(message.NormalMessageId)
	msg.Data = result
	w.WriteJson(msg)

}
